﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using TByte.XnaHelper.Input;

namespace TByte.XnaHelper.GameObjects
{
    public class ToggleButtonGameObject : GameObject
    {
        #region Fields
        SpriteSheet sheet;
        int currentValue;
        List<string> valueNames;
        List<dynamic> values;
        SpriteFont font;
        Rectangle rect;
        Color fontColor;
        public event Action ValueChanged;
        bool hover;
        #endregion

        #region Methods
        public override void Update()
        {
            rect = new Rectangle((int)Position.X, (int)Position.Y, Width, Height);
            if (rect.Contains(InputSystem.MouseLocation))
            {
                hover = true;
            }
            else
            {
                hover = false;
            }
            base.Update();
        }
        public override void Draw(SpriteBatch spriteBatch)
        {
            if (sheet != null)
                sheet.Draw(spriteBatch, Position, hover ? 1 : 0);
            else if (hover)
                spriteBatch.Draw(Manager.dummyTexture, rect, Color.Black * 0.5f);
            spriteBatch.DrawString(font, valueNames[currentValue % valueNames.Count()], Position + (new Vector2(rect.Width, rect.Height) - font.MeasureString(valueNames[currentValue % valueNames.Count()])) / 2, fontColor);
            base.Draw(spriteBatch);
        }
        public override void OnMouseDown(Input.MouseEventArgs e)
        {
            if (hover)
            {
                if (e.Button == MouseButton.Left)
                {
                    currentValue++;
                }
                else if (e.Button == MouseButton.Right)
                {
                    currentValue--;
                    if (currentValue < 0)
                        currentValue += values.Count();
                }
                if (ValueChanged != null)
                    ValueChanged();
            }
            base.OnMouseDown(e);
        }
        #endregion

        #region Constructors
        public ToggleButtonGameObject(
            SpriteSheet sheet,
            List<dynamic> values,
            List<string> valueNames,
            int defaultValue,
            SpriteFont font,
            Color fontColor,
            string name)
            : base(name)
        {
            this.sheet = sheet;
            this.values = values;
            this.valueNames = valueNames;
            this.currentValue = defaultValue;
            this.font = font;
            this.fontColor = fontColor;
        }
        #endregion

        #region Properties
        public dynamic Value { get { return values[currentValue % values.Count]; } }
        public int CurrentValue { get { return currentValue; } set { currentValue = value; } }
        public List<dynamic> ValueList { get { return values; } set { values = value; } }
        public List<string> ValueNames { get { return valueNames; } set { valueNames = value; } }
        public int Height { get { return sheet == null ? (int)font.MeasureString(valueNames[currentValue]).Y + 10 : sheet.Height(0); } }
        public int Width { get { return sheet == null ? (int)font.MeasureString(valueNames[currentValue]).X + 20 : sheet.Width(0); } }
        #endregion
    }
}
